import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserInfo } from '@/api/request.js'

// 用户模块
export const useUserStore = defineStore(
  'big-user',
  () => {
    const token = ref('') // 定义 token
    const setToken = (t) => (token.value = t) // 设置 token

    //移除token
    const removeToken = () => {
      token.value = ''
    }

    // 用户基本信息
    const user = ref({})
    // 获取用户基本信息
    const saveUserInfo = async () => {
      const { data } = await getUserInfo()
      user.value = data
    }
    // 设置用户信息
    const setUserInfo = (newUser) => {
      user.value = newUser
    }

    return { token, user, setToken, removeToken, saveUserInfo, setUserInfo }
  },
  {
    persist: true // 持久化
  }
)
